package cn.lvhang.system.controller;

import cn.lvhang.basic.util.AjaxResult;
import cn.lvhang.basic.util.PageList;
import cn.lvhang.system.domain.Detail;
import cn.lvhang.system.domain.Type;
import cn.lvhang.system.query.TypeQuery;
import cn.lvhang.system.service.IDetailService;
import cn.lvhang.system.service.ITypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // @Controller +@ResponseBody
@RequestMapping("/detail")
@Api(tags = "员工接口",description = "员工接口详细描述")//用于对swagger字段的描述
public class DetailController {
    @Autowired
    private IDetailService detailService;
    //新增或修改
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")//用于对swagger字段的描述
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        try {
            if(detail.getId() == null){
                detailService.add(detail);
            }else{
                detailService.update(detail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }

    }

    //删除
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            detailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }
    //查询单个
    @GetMapping("/{id}")
    public Detail queryById(@PathVariable("id") Long id){
        return detailService.queryById(id);
    }
    //查询所有
    @GetMapping
    public List<Detail> queryAll(){
        return detailService.queryAll();
    }

    //分页 + 高级查询
    // @RequestBody接收前台发送的json字符串
    @PostMapping
    public PageList<Detail> queryPage(@RequestBody TypeQuery query){
        return detailService.queryPage(query);
    }
    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            detailService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("操作失败！");
        }
    }






}
